package com.example.cyh.bczapp.Code.BLL;


import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.widget.Toast;

import com.example.cyh.bczapp.Code.Activity.MainActivity;
import com.example.cyh.bczapp.Code.Helper.HttpHelper;
import com.example.cyh.bczapp.Code.Model.PlanInfEntity;
import com.example.cyh.bczapp.Code.Model.UserInfEntity;
import com.example.cyh.bczapp.Code.Model.WordGroupInfEntity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;


/**
 * Created by cyh on 2017/5/7.
 */

public class UserBLL {
    private final String BaseUrl = "http://123.207.86.30/public/index.php/db";

    public UserInfEntity Login(String id, String pwd) {
        //参数制作
        Dictionary<String, String> param = new Hashtable();
        param.put("id", id);
        param.put("pwd", pwd);
        try {
            String restult = HttpHelper.POST(BaseUrl + "/Usercontrol/UserLogin", param);
            if (restult == "id||pwd error") {
                return null;
            }
            Gson gson = new Gson();
            UserInfEntity user = gson.fromJson(restult, UserInfEntity.class);
            return user;
        } catch (Exception exc) {
            return null;
        }
    }

    public Boolean AddPlanId(String p_id, int u_id) {
        Dictionary<String, String> param = new Hashtable();
        param.put("plan_id", p_id);
        param.put("user_id", String.valueOf(u_id));
        try {
            String restult = HttpHelper.POST(BaseUrl + "/Usercontrol/Addplanid", param);
            return Boolean.valueOf(restult);
        } catch (Exception exc) {
            return false;
        }
    }

    public ArrayList<PlanInfEntity> GetAllPlan(int u_id) {
        Dictionary<String, String> param = new Hashtable();
        param.put("u_id", String.valueOf(u_id));
        try {
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<PlanInfEntity>>() {
            }.getType();
            String back = HttpHelper.POST(BaseUrl + "/Usercontrol/GetUserPlan", param);
            return gson.fromJson(back, type);
        } catch (Exception exc) {
            return null;
        }
    }

    public boolean OverToday(String u_id, String p_id) {
        Dictionary<String, String> param = new Hashtable();
        param.put("u_id", String.valueOf(u_id));
        param.put("p_id", String.valueOf(p_id));
        try {
            String back = HttpHelper.POST(BaseUrl + "/Usercontrol/OverToday", param);
            return back == "1";
        } catch (Exception exc) {
            return false;
        }
    }

    public Boolean AddUser(UserInfEntity userInfEntity) {
        Gson gson = new Gson();
        Dictionary<String, String> param = new Hashtable();
        param.put("rd", String.valueOf(gson.toJson(userInfEntity)));
        try {
            String back = HttpHelper.POST(BaseUrl + "/Usercontrol/Add", param);
            return back.equals("true");
        } catch (Exception exc) {
            return false;
        }

    }

    public String SetPlanEmpty(String id) {
        String url = BaseUrl + "Usercontrol/SetPlanEmpty";
        Dictionary<String, String> param = new Hashtable();
        param.put("u_id", id);
        try {
            String back = HttpHelper.POST(url, param);
            return back;
        } catch (Exception ex) {
            return "false";
        }
    }
    public UserInfEntity IdGetUser(String id)
    {
        Dictionary<String, String> param = new Hashtable();
        param.put("u_id", id);
        try {
            String restult = HttpHelper.POST(BaseUrl + "/Usercontrol/IdGetUser", param);
            Gson gson = new Gson();
            UserInfEntity users = gson.fromJson(restult, UserInfEntity.class);
            return users;
        } catch (Exception exc) {
            return null;
        }
    }
}
